﻿using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using Yoozoo.Core.Common;


namespace Yoozoo.Gameplay.Liberty.Nodes
{
    public class Switch2Scene : LatentActionNodeBase
    {
        private bool finish = false;
        public override bool allowRoutineQueueing { get { return false; } }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<string>("SceneName");
            var p2 = node.AddValueInput<string>("SceneParams");
            
            node.AddFlowInput("In", (f) => { Begin(Invoke(p1.value, p2.value), f); });

        }
        
        private IEnumerator Invoke(string sceneName, string sceneParams)
        {
            finish = false;
            
            //需要优先判定当前是否在加载新的场景中
            yield return new UnityEngine.WaitUntil(() =>
            {
                return LoadingUtils.LoadingTaskManager.enterSceneSuccess;
            });
            
            BlueprintUtils.Switch2Scene(sceneName, sceneParams, OnFinish);
            yield return new UnityEngine.WaitUntil(() =>
            {
                return finish;
            });
        }
        
        private void OnFinish()
        {
            finish = true;
        }

    }
}